package mil.af.amc.adpetracker.model;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;


/**
 * Application User class
 *
 * @author SrA Schevus Osborne
 */

@Entity
@Table(name="T_USER")
public class User implements Serializable, Comparable<User> {

    private static Log log = LogFactory.getLog(User.class);

    private int id;
    private Account account;
    private String firstName;
    private String lastName;
    private String comm;
    private String dsn;
    private String email;
    private String organization;
    private CustodianType custodianType;
    private Organization assignedOrganization;
    private List<EquipmentItem> assignedEquipment;

    @Id
    @Column(name="USER_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_USER")
    @SequenceGenerator(name="SEQ_USER", sequenceName = "S_USER")
    public int getId() {
        return id;
    }

    @OneToOne
    @PrimaryKeyJoinColumn
    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    @Column(name = "DSNPHONE")
    public String getDsn() {
        return dsn;
    }

    public void setDsn(String dsn) {
        this.dsn = dsn;
    }

    @Column(name = "COMMPHONE")
    public String getComm() {
        return comm;
    }

    public void setComm(String comm) {
        this.comm = comm;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "FIRST_NAME")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "LAST_NAME")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(name = "ORGANIZATION")
    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    @Enumerated
    @Column(name="CUSTODIANTYPE")
    public CustodianType getCustodianType() {
        return custodianType;
    }

    public void setCustodianType(CustodianType custodianType) {
        this.custodianType = custodianType;
    }

    @Column(name = "EMAIL")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @ManyToOne
    @JoinColumn(name="ORGANIZATIONID")
    public Organization getAssignedOrganization() {
        return assignedOrganization;
    }

    public void setAssignedOrganization(Organization assignedOrganization) {
        this.assignedOrganization = assignedOrganization;
    }


    @OneToMany(mappedBy="assignedUser")
    //@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    //@JoinColumn(name="USERID")
    public List<EquipmentItem> getAssignedEquipment() {
        return assignedEquipment;
    }

    public void setAssignedEquipment(List<EquipmentItem> assignedEquipment) {
        this.assignedEquipment = assignedEquipment;
    }

    @Transient
    public String getDisplayName() {

        StringBuffer sb = new StringBuffer();
        sb.append(this.getLastName());
        sb.append(", ");
        sb.append(this.getFirstName());

        return sb.toString();
    }

    @Transient
    public int getItemCount() {
        return assignedEquipment.size();
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final User user = (User) o;

        if (!email.equals(user.email)) return false;

        return true;
    }

    public int hashCode() {
        return email.hashCode();
    }

    public int compareTo(User o) {

        return this.getDisplayName().compareTo(o.getDisplayName());

    }
}
